package com.wskeee.ec.styles
{
	import com.wskeee.ec.core.EGlobal;
	import com.wskeee.ec.utils.PolygonDrawer;
	
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	public class Icons
	{
		//--------------------------------------------------------------------------
		//
		//  组件默念 icon
		//
		//--------------------------------------------------------------------------
		public static var icons:Dictionary = new Dictionary();
		//--------------------------------------------------------------------------
		//
		//  arrow icon
		//
		//--------------------------------------------------------------------------
		public static var arrows:Vector.<Object> = new Vector.<Object>(8);
		
		//--------------------------------------------------------------------------
		//
		//  RadioButton
		//
		//--------------------------------------------------------------------------
		public static var radioButtonIcon:Object;			//正常图标
		public static var radioButtonSelectedIcon:Object;	//选择图标
		//--------------------------------------------------------------------------
		//
		//  CheckBox
		//
		//--------------------------------------------------------------------------
		public static var checkBoxIcon:Object;				//正常图标
		public static var checkBoxSelectedIcon:Object;		//选择图标
		//--------------------------------------------------------------------------
		//
		//  滚动条 icon
		//
		//--------------------------------------------------------------------------
		//-----------------------------------------------------------------
		//  VScrollBar
		//-----------------------------------------------------------------
		
		public static var vScrollArrowUpIcon:Object;
		public static var vScrollArrowDownIcon:Object;
		public static var vScrollThumbIcon:Object;
		
		//-----------------------------------------------------------------
		//  HScrollBar
		//-----------------------------------------------------------------
		
		public static var hScrollArrowUpIcon:Object;
		public static var hScrollArrowDownIcon:Object;
		public static var hScrollThumbIcon:Object;
		
		//--------------------------------------------------------------------------
		//
		//  Tree
		//
		//--------------------------------------------------------------------------
		
		public static var treeSwitchOpenIcon:Object;	//打开
		public static var treeSwitchCloseIcon:Object;	//关闭
		public static var treeFolderOpenIcon:Object;	//打开文件夹
		public static var treeFolderCloseIcon:Object; 	//关闭文件夹
		public static var treeLeafIcon:Object;			//叶子
		
		//--------------------------------------------------------------------------
		//
		//  ComboBox
		//
		//--------------------------------------------------------------------------
		
		public static var comboOpenButtonIcon:Object;
		
		//--------------------------------------------------------------------------
		//
		//  static method
		//
		//--------------------------------------------------------------------------
		public static function createDefaultIcon(useDraw:Boolean = true):void
		{
			if(useDraw)
			{
				drawArrow();
				drawScrollBarIcon();
				drawRadioButtonIcon();
				drawCheckBoxIcon();
				drawFolder();
				drawLeaf();
			}
			
			//---------------------
			//radioButton
			//---------------------
			radioButtonIcon = icons["radioButtonIcon"];
			radioButtonSelectedIcon = icons["radioButtonSelectedIcon"];
			//---------------------
			//CheckButton
			//---------------------
			checkBoxIcon = icons["checkBoxIcon"];
			checkBoxSelectedIcon = icons["checkBoxSelectedIcon"];
			//---------------------
			//VScrollBar
			//---------------------
			vScrollArrowUpIcon = icons["arrowUpIcon"];
			vScrollArrowDownIcon = icons["arrowDownIcon"];
			vScrollThumbIcon = icons["vScrollThumbIcon"];
			
			//---------------------
			//HScrollBar
			//---------------------
			hScrollArrowUpIcon = icons["arrowLeftIcon"];
			hScrollArrowDownIcon = icons["arrowRightIcon"];
			hScrollThumbIcon = icons["hScrollThumbIcon"];
			//---------------------
			//Tree
			//---------------------
			treeSwitchOpenIcon = icons["treeSwitchOpenIcon"] ;
			treeSwitchCloseIcon = icons["treeSwitchCloseIcon"];
			
			treeFolderOpenIcon = icons["treeFolderOpenIcon"] ;
			treeFolderCloseIcon = icons["treeFolderCloseIcon"];
			treeLeafIcon = icons["treeLeafIcon"];
		}
		
		public static function createIconByIDE():void
		{
			if(EGlobal.resourceManager.getSkinByName("ecskin"))
			{
				//---------------------
				//arrowIcon
				//---------------------
				icons["arrowUpIcon"] = EGlobal.resourceManager.getElementClass("ArrowUp_Icon","ecskin");
				icons["arrowRightUpIcon"] = EGlobal.resourceManager.getElementClass("ArrowRightUp_Icon","ecskin");
				icons["arrowRightIcon"] = EGlobal.resourceManager.getElementClass("ArrowRight_Icon","ecskin");
				icons["arrowRightDownIcon"] = EGlobal.resourceManager.getElementClass("ArrowRightDown_Icon","ecskin");
				icons["arrowDownIcon"] = EGlobal.resourceManager.getElementClass("ArrowDown_Icon","ecskin");
				icons["arrowLeftDownIcon"] = EGlobal.resourceManager.getElementClass("ArrowLeftDown_Icon","ecskin");
				icons["arrowLeftIcon"]= EGlobal.resourceManager.getElementClass("ArrowLeft_Icon","ecskin");
				icons["arrowLeftUpIcon"]= EGlobal.resourceManager.getElementClass("ArrowLeftUp_Icon","ecskin");
				//---------------------
				//Tree
				//---------------------
				icons["treeSwitchOpenIcon"] = EGlobal.resourceManager.getElementClass("TreeSwitchOpen_Icon","ecskin");
				icons["treeSwitchColseIcon"] = EGlobal.resourceManager.getElementClass("TreeSwitchColse_Icon","ecskin");
				
				icons["treeFolderOpenIcon"] = EGlobal.resourceManager.getElementClass("TreeFolderOpen_Icon","ecskin");
				icons["treeFolderCloseIcon"] = EGlobal.resourceManager.getElementClass("TreeFolderClose_Icon","ecskin");
				icons["treeLeafIcon"] = EGlobal.resourceManager.getElementClass("TreeLeaf_Icon","ecskin");
				createDefaultIcon(false);
			}else
			{
				createDefaultIcon(true);
			}
		}
		
		/**
		 * 画8个方向箭头,可用做 滚动条(ScrollBar)上下按钮icon、
		 * 下拉框(ComboBox)的openButton icon、
		 * 树(Tree)里面的 开关 icon
		 * 
		 */
		private static function drawArrow():void
		{
			var names:Array = ["arrowUpIcon","arrowRightUpIcon","arrowRightIcon","arrowRightDownIcon",
			"arrowDownIcon","arrowLeftDownIcon","arrowLeftIcon","arrowLeftUpIcon"];
			var w:int = 10,h:int=10,s:int= Math.sqrt((w/2)*(w/2)+(h/2)*(h/2));
			
			var datas:Vector.<Array> = new <Array>[
				//w,h,p1,p2,p3
				[new Point(w,h/2),new Point(0,h/2),new Point(w/2,0),new Point(w,h/2)],
				[new Point(s,s),new Point(0,0),new Point(s,0),new Point(s,s)],
				[new Point(w/2,h),new Point(0,0),new Point(w/2,h/2),new Point(0,h)],
				[new Point(s,s),new Point(s,0),new Point(s,s),new Point(0,s)],
				[new Point(w,h/2),new Point(0,0),new Point(w,0),new Point(w/2,h/2)],
				[new Point(s,s),new Point(0,0),new Point(s,s),new Point(0,s)],
				[new Point(w/2,h),new Point(w/2,0),new Point(w/2,h),new Point(0,h/2)],
				[new Point(s,s),new Point(s,0),new Point(0,s),new Point(0,0)]
			];
			var icon:Shape = new Shape();
			var g:Graphics = icon.graphics;
			var bmd:BitmapData;
			for(var i:int=0,len:int=datas.length;i<len;i++)
			{
				bmd = new BitmapData(datas[i][0].x,datas[i][0].y,true,0);
				g.clear();
				g.beginFill(themeSelectedColor,0.8);
				g.moveTo(datas[i][1].x,datas[i][1].y);
				g.lineTo(datas[i][2].x,datas[i][2].y);
				g.lineTo(datas[i][3].x,datas[i][3].y);
				g.endFill();
				bmd.draw(icon);
				icons[names[i]] = bmd;
			}
			icons["treeSwitchOpenIcon"] = icons["arrowRightDownIcon"];
			icons["treeSwitchCloseIcon"] = icons["arrowRightIcon"];
		}
		
		private static function drawScrollBarIcon():void
		{
			var bmd:BitmapData;
			icons["vScrollThumbIcon"] = PolygonDrawer.drawThumbIcon(10,10,[themeSelectedColor,themeColor],[1,1],[255,127]);
			var m:Matrix = new Matrix();
			m.rotate(90/180*Math.PI);
			m.translate(10,0);
			bmd = new BitmapData(10,10,true,0);
			bmd.draw(icons["vScrollThumbIcon"],m);
			icons["hScrollThumbIcon"] = bmd;
		}
		
		/**
		 * 文件夹图标,用作树形列表icon 
		 * @return 
		 * 
		 */
		private static function drawFolder():void
		{
			var s:int = 3;
			var w:int = 18,h:int=12;
			var icon:Shape = new Shape();
			var g:Graphics = icon.graphics;
			var bmd:BitmapData;
			var commands:Vector.<int> = new <int>[1,2,2,2,2,2,2,2];
			var datas:Vector.<Number> = new <Number>[0,s, s,0, 2*s,0, 3*s,s, w,s, w,h, 0,h, 0,s];
			g.lineStyle(0.1,0,0.5);
			g.beginFill(0xcccccc,1);
			g.drawPath(commands,datas);
			bmd = new BitmapData(w+1,h+1,true,0);
			bmd.draw(icon,new Matrix(1,0,0,1,0.5,0.5),null,null,null,true);
			icons["treeFolderOpenIcon"] = icons["treeFolderCloseIcon"] = bmd;
		}
		/**
		 * 文件图标,用作树形列表icon 
		 * @return 
		 * 
		 */
		private static function drawLeaf():void
		{
			var s:int = 3;
			var w:int = 12,h:int=12;
			var icon:Shape = new Shape();
			var g:Graphics = icon.graphics;
			var bmd:BitmapData;
			var commands:Vector.<int> = new <int>[1,2,2,2,2,2];
			var datas:Vector.<Number> = new <Number>[0,0, w-s,0, w,s, w,h, 0,h, 0,0];
			g.lineStyle(0.1,themeSelectedColor,1,false,LineScaleMode.NONE);
			g.beginFill(0xffffff,1);
			g.drawPath(commands,datas);
			bmd = new BitmapData(w+1,h+1,true,0);
			var m:Matrix = new Matrix(1,0,0,1,0.5,0.5);
			bmd.draw(icon,m,null,null,null,true);
			icons["treeLeafIcon"] = bmd;
		}
		
		/**
		 * RadioButton 正常与选择图标 
		 * 
		 */
		private static function drawRadioButtonIcon():void
		{
			var w:int=13,h:int=13;
			var icon:Shape = new Shape();
			var g:Graphics = icon.graphics;
			var bmd:BitmapData;
			var m:Matrix = new Matrix(1,0,0,1,0.5,0.5);
			g.lineStyle(0.1,themeSelectedColor,1,false,LineScaleMode.NONE);
			g.beginFill(0xffffff,1);
			g.drawCircle(w/2,h/2,w/2-0.2);
			bmd = new BitmapData(w+1,h+1,true,0);
			
			bmd.draw(icon,m,null,null,null,true);
			icons["radioButtonIcon"] = bmd;
			g.endFill();
			g.beginFill(themeSelectedColor,1);
			g.drawCircle(w/2,h/2,2);
			bmd = new BitmapData(w+1,h+1,true,0);
			bmd.draw(icon,m,null,null,null,true);
			icons["radioButtonSelectedIcon"] = bmd;
		}
		
		/**
		 * CheckBox 正常与选择图标 
		 * 
		 */
		private static function drawCheckBoxIcon():void
		{
			var w:int=13,h:int=13;
			var icon:Shape = new Shape();
			var g:Graphics = icon.graphics;
			var bmd:BitmapData;
			var m:Matrix = new Matrix(1,0,0,1,0.5,0.5);
			g.lineStyle(0.1,themeSelectedColor,1,false,LineScaleMode.NONE);
			g.drawRect(0,0,w,h);
			g.beginFill(0xffffff,1);
			bmd = new BitmapData(w+1,h+1,true,0);
			bmd.draw(icon,m,null,null,null,true);
			icons["checkBoxIcon"] = bmd;
			
			g.lineStyle(2,themeSelectedColor);
			g.drawPath(new <int>[1,2,2],new <Number>[4,7, 6,11, 12,2]);
			bmd = new BitmapData(w+1,h+1,true,0);
			bmd.draw(icon,m,null,null,null,true);
			icons["checkBoxSelectedIcon"] = bmd;
		}
		
		/**
		 * 获取 icon 
		 * @param name
		 * @return 
		 * 
		 */
		public static function getIconByName(name:String):BitmapData
		{
			return icons[name];
		}
		
		private static function get themeColor():uint
		{
			return EGlobal.styleManager.getStyle("global")["themeColor"];
		}
		private static function get themeSelectedColor():uint
		{
			return EGlobal.styleManager.getStyle("global")["themeSelectedColor"];
		}
	}
}